#import "ALElementBarPopUpButtonCell.h"


#pragma mark Class variables
static NSColor *ALElementBarBorderColor;
static NSColor *ALElementBarBorderColorSelected;
static NSGradient *ALElementBarBackgroundGradient;
static NSGradient *ALElementBarBackgroundGradientSelected;
static NSGradient *ALElementBarBackgroundGradientPressed;
static NSGradient *ALElementBarBackgroundGradientPressedSelected;


#pragma mark -
@implementation ALElementBarPopUpButtonCell

@synthesize selected;


+(void)initialize {
	ALElementBarBorderColor = [[NSColor colorWithCalibratedWhite:0.783 alpha:1.000] retain];
	ALElementBarBorderColorSelected = [[NSColor colorWithCalibratedWhite:0.5f alpha:1.0f] retain];
	NSColor *backgroundTop = [NSColor colorWithCalibratedWhite:0.944 alpha:1.000];
	NSColor *backgroundBottom = [NSColor colorWithCalibratedWhite:1.000 alpha:1.000];
	ALElementBarBackgroundGradient = [[NSGradient alloc] initWithStartingColor:backgroundTop endingColor:backgroundBottom];
	NSColor *bottomSelected = [NSColor colorWithCalibratedRed:0.864 green:0.895 blue:0.926 alpha:1.000];
	NSColor *topSelected = [NSColor colorWithCalibratedRed:0.664 green:0.712 blue:0.833 alpha:1.000];
	ALElementBarBackgroundGradientSelected = [[NSGradient alloc] initWithStartingColor:topSelected endingColor:bottomSelected];
	NSColor *pressedTop = [NSColor colorWithCalibratedWhite:0.6 alpha:1.0f];
	NSColor *pressedBottom = [NSColor colorWithCalibratedWhite:0.8 alpha:1.0f];
	ALElementBarBackgroundGradientPressed = [[NSGradient alloc] initWithStartingColor:pressedTop endingColor:pressedBottom];
	NSColor *pressedSelectedBottom = [NSColor colorWithCalibratedRed:0.664 green:0.695 blue:0.726 alpha:1.000];
	NSColor *pressedSelectedTop = [NSColor colorWithCalibratedRed:0.464 green:0.512 blue:0.633 alpha:1.000];
	ALElementBarBackgroundGradientPressedSelected = [[NSGradient alloc] initWithStartingColor:pressedSelectedTop endingColor:pressedSelectedBottom];
}

-(void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView {
	NSBezierPath *borderPath = [NSBezierPath bezierPathWithRect:NSInsetRect(frame, 0.5f, 0.5f)];
	NSGradient *backgroundGradient = [self selected] ? ALElementBarBackgroundGradientSelected : ALElementBarBackgroundGradient;
	NSGradient *pressedBackgroundGradient = [self selected] ? ALElementBarBackgroundGradientPressedSelected : ALElementBarBackgroundGradientPressed;
	NSGradient *currentGradient = [self isHighlighted] ? pressedBackgroundGradient : backgroundGradient;
	[currentGradient drawInBezierPath:borderPath angle:270.0f];
	NSColor *borderColor = [self selected] ? ALElementBarBorderColorSelected : ALElementBarBorderColor;
	[borderColor setStroke];
	[borderPath stroke];
	
	if ([self showsFirstResponder] && [self focusRingType] != NSFocusRingTypeNone) {
		NSBezierPath *focusPath = [NSBezierPath bezierPathWithRect:frame];
		[NSGraphicsContext saveGraphicsState];
		NSSetFocusRingStyle(NSFocusRingOnly);
		[focusPath fill];
		[NSGraphicsContext restoreGraphicsState];		
	}
}

@end
